package com.org.gundutubruk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class NewPlayer extends Activity{

	int currApatar=R.drawable.apatar0;
	int noApatar;
	boolean isApatarCek=false;
	
	Button apatarkiri;
	Button apatarkanan;
	ImageView gbrApatar;
	EditText nama;
	int jumGundu = 5;
	int level;
	Button buat;
	
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);    	
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	setContentView(R.layout.new_player_layout);
    	if(!Utility.checkPlayerHolderAlreadyExist(this)){
			Utility.playerHolder=new PlayerHolder();
		}
    	castItem();
    	gbrApatar.setImageResource(currApatar);
    	noApatar=0;    	
    	isiFungsiItem();
	}
	
	private void castItem(){
		apatarkanan=(Button)findViewById(R.id.apatarkanan);
		apatarkiri=(Button)findViewById(R.id.apatarkiri);
		gbrApatar=(ImageView)findViewById(R.id.pilihapatar);
		nama=(EditText)findViewById(R.id.isinama);
		nama.setSingleLine();
		buat=(Button)findViewById(R.id.buat);
	}
	
	private void isiFungsiItem(){
		apatarkanan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(noApatar<4){
					noApatar++;
					currApatar++;
					gbrApatar.setImageResource(currApatar);
				}
			}
		});
		
		apatarkiri.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(noApatar>0){
					noApatar--;
					currApatar--;
					gbrApatar.setImageResource(currApatar);
				}
			}
		});
		buat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String nem=nama.getText().toString();
				Toast toast;
				if(!nem.equals("")&&cekNem(nem)&&cekNemLen(nem)){
					Player P = new Player(nem, noApatar, Utility.playerHolder.size());
					Utility.playerHolder.add(P);
					nama.setText("");
					//kasih pesan kalo player berhasil dibuat
					toast=Toast.makeText(getApplicationContext(), "Pemain berhasil dibuat", Toast.LENGTH_SHORT);
					toast.show();
					Log.d("dfd", "Jumlah sekarang : "+Utility.playerHolder.size());
					Utility.serializePlayerHolder(getApplicationContext());
				}else{
					//kasih pesan buat ngisi data yg bener
					if(nem.equals("")){
						toast=Toast.makeText(getApplicationContext(), "Isi nama pemain", Toast.LENGTH_SHORT);
						toast.show();
					}else if(!cekNem(nem)){
						toast=Toast.makeText(getApplicationContext(), "Nama sudah ada", Toast.LENGTH_SHORT);
						toast.show();
					}else if(!cekNemLen(nem)){
						toast=Toast.makeText(getApplicationContext(), "Maksimal huruf pada nama adalah 8 huruf", Toast.LENGTH_SHORT);
						toast.show();
					}else{
						toast=Toast.makeText(getApplicationContext(), "Terdapat kesalahan", Toast.LENGTH_SHORT);
						toast.show();
					}
				}
			}
		});
	}
	protected boolean cekNemLen(String nem) {
		if(nem.length()>8)
			return false;
		return true;
	}

	private void nextAct2() {
		this.startActivity(new Intent("android.intent.action.GAMESETTING"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		Utility.serializePlayerHolder(this);
		this.finish();
	}
	private boolean cekNem(String nama){
		boolean retval = true;
		for(Player P: Utility.playerHolder.AllPlayer){
			if(P.name.equals(nama)){
				retval=false;
				break;
			}
		}
		return retval;
	}
	public void onBackPressed(){
		if(getIntent().getBooleanExtra("fromMultiplayer", false)) {//jika dari multiplayer
			finish();		
		}else{
			nextAct2();
		}
	}
}
